package pushtest.mobike.com.mtpush.ui

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import kotlinx.android.synthetic.main.activity_scrolling.*
import kotlinx.android.synthetic.main.content_scrolling.*
import pushtest.mobike.com.mtpush.R
import pushtest.mobike.com.mtpush.ui.adapter.MyMultiItem
import pushtest.mobike.com.mtpush.ui.behavior.MyTestBehaivor
import pushtest.mobike.com.mtpush.ui.adapter.ScrollingSectionAdapter
import pushtest.mobike.com.mtpush.ui.behavior.LocationBehavior
import pushtest.mobike.com.mtpush.utils.dp2px
import pushtest.mobike.com.mtpush.utils.snackbar
import pushtest.mobike.com.mtpush.utils.toast
import timber.log.Timber

class ScrollingActivity : AppCompatActivity() {
  lateinit var adapter: ScrollingSectionAdapter
  lateinit var header: View
  lateinit var myTestBehavior: MyTestBehaivor<RecyclerView>
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_scrolling)
    setSupportActionBar(toolbar)
//    fab.setOnClickListener { view ->
//      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//        .setAction("Action", null).show()
//    }
    header = LayoutInflater.from(this).inflate(R.layout.layout_top_tip, null);
    var num = 0
    location.setOnClickListener {
      layout_tip.animate().translationY(0F).alpha(1F)
        .start()
      addHeader()
      LocationBehavior.from(location).setState(LocationBehavior.FIRST)
    }
    initRecyclerView()
  }

  private fun addHeader() {
    //      adapter.setHeaderView(header)
    //      header.findViewById<TextView>(R.id.tip).animate().translationY(0F).start()
    //      design_bottom_sheet.scrollToPosition(0)
  }

  override fun onResume() {
    super.onResume()
    Timber.w("onResume")
//    location.postDelayed(
//      { LocationBehavior.from(location).setState(LocationBehavior.FIRST) }
//      , 100)

  }

  private fun initRecyclerView() {

    initRecyclerView(setupAdapter(), setupBehavior())

  }

  private fun setupAdapter(): ScrollingSectionAdapter {
    val lists = mutableListOf<MyMultiItem>()
    for (i in 1..20) {
      when (i % 3) {
        0 -> lists.add(MyMultiItem(MyMultiItem.tip, "这是tip $i"))
        1 -> lists.add(MyMultiItem(MyMultiItem.unlock, "这是开锁"))
        2 -> lists.add(
          MyMultiItem(
            MyMultiItem.ad,
            "https://cache.bestmvno.com/wp-content/uploads/2016/02/Verizon-FreeBee-Data-1-1080x280.jpg"
          )
        )
      }

    }
//    val adapter = ScrollingViewAdapter(android.R.layout.simple_list_item_1, lists)
    adapter = ScrollingSectionAdapter(lists,
      object : AnimatorListenerAdapter() {
        override fun onAnimationStart(animation: Animator?) {
          LocationBehavior.from(location).setState(LocationBehavior.FIRST)
//          location.animate().translationY(this@ScrollingActivity.dp2px(-60).toFloat()).start()
        }
      })


    adapter.setOnItemClickListener { adapter, view, position ->
      view.context.toast("setOnItemChildClickListener $position")
    }
    adapter.setOnItemChildClickListener { adapter, view, position ->
      when (view.id) {
        R.id.unlcok ->
          view.snackbar("开锁")
        R.id.report ->
          view.snackbar("上报故障")
        R.id.service ->
          view.snackbar("客服服务")
      }
    }
    adapter.setNotDoAnimationCount(3)//设置不显示动画数量
//    adapter.setPreLoadNumber(2)
    adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
    return adapter
  }


  private fun initRecyclerView(
    adapter: ScrollingSectionAdapter,
    behavior: MyTestBehaivor<RecyclerView>?
  ) {
    design_bottom_sheet.setHasFixedSize(true)
    design_bottom_sheet.layoutManager = LinearLayoutManager(this)
    design_bottom_sheet.adapter = adapter
    design_bottom_sheet.addOnScrollListener(object : RecyclerView.OnScrollListener() {
      override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
        Timber.i("dx = $dx  dy = $dy")
      }

      override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        Timber.i(" new State = $newState")
      }
    })
//    design_bottom_sheet.postDelayed({
//      behavior?.peekHeight = design_bottom_sheet.context.dp2px(500)
//    }, 1000)
  }

  private fun setupBehavior(): MyTestBehaivor<RecyclerView>? {

    myTestBehavior = MyTestBehaivor.from(design_bottom_sheet)
//    behavior.peekHeight = this@ScrollingActivity.dp2px(300)
    myTestBehavior.setBottomSheetCallback(object : MyTestBehaivor.BottomSheetCallback() {
      override fun onSlide(view: View, p1: Float) {
        Timber.i(
          "onSlide  $p1"
        )
        if (view.top < this@ScrollingActivity.dp2px(100)) {
          root_view.setBackgroundColor(Color.parseColor("#88000000"))
        } else {
          root_view.setBackgroundColor(Color.TRANSPARENT)
        }
        if (p1 == 1F) {
          Timber.i("height = ${view.height}   rawY = ${view.y}   ${view.top}")
        }

      }

      override fun onStateChanged(p0: View, p1: Int) {
        Timber.i(" p1 =  $p1")

      }

    })
    return myTestBehavior
  }


  companion object {

  }
}
